package com.qiaofang.collectmanage.service.common.utils;

import com.gargoylesoftware.htmlunit.*;
import com.qiaofang.collectmanage.service.common.bean.ProxyIP;
import com.qiaofang.collectmanage.service.common.bean.RequestResponseInfo;
import com.qiaofang.collectmanage.service.common.constant.MetricsCons;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class WuHttpClient {
    private static final Logger logger = LoggerFactory.getLogger(WuHttpClient.class);

    private WuHttpClient() {
    }

    long sleepMs = 200;
    boolean useJs = false;
    String targetUrl = "";
    int timeOut = 5000;
    ProxyIP proxyIP = null;
    String codeType = "";
    String referer;
    boolean https;
    boolean outputHeaderInfo;

    public WuHttpClient(long sleepMs, String targetUrl, ProxyIP proxyIP, boolean https, String codeType) {
        this.sleepMs = sleepMs;
        this.targetUrl = targetUrl;
        this.useJs = false;
        this.timeOut = 5000;
        this.proxyIP = proxyIP;
        this.referer = "";
        this.https = https;
        this.codeType = codeType;
        this.outputHeaderInfo = true;
    }

    public RequestResponseInfo webParseHtml(String url) {
        MetricUtils.send(MetricsCons.HTTP_REQUEST_OUT);
        RequestResponseInfo resultBean = new RequestResponseInfo();
        String pageText = "";

        BrowserVersion[] versions = {BrowserVersion.CHROME, BrowserVersion.FIREFOX_38, BrowserVersion.INTERNET_EXPLORER_11, BrowserVersion.INTERNET_EXPLORER_8};
       // BrowserVersion[] versions = {BrowserVersion.CHROME, BrowserVersion.FIREFOX_38, BrowserVersion.INTERNET_EXPLORER_11};
        WebClient client = new WebClient(versions[(int) (versions.length * Math.random())]);
        try {
            client.getOptions().setThrowExceptionOnFailingStatusCode(false);
            client.getOptions().setJavaScriptEnabled(useJs);
            client.getOptions().setCssEnabled(false);
            client.getOptions().setThrowExceptionOnScriptError(false);
            client.getOptions().setTimeout(timeOut);

            client.getOptions().setUseInsecureSSL(https);

            if (referer != null && !"".equals(referer)) {
                client.addRequestHeader("Referer", referer);
            }
            if (proxyIP != null) {
                ProxyConfig proxyConfig = new ProxyConfig(proxyIP.getIp(), Integer.valueOf(proxyIP.getPort()));
                client.getOptions().setProxyConfig(proxyConfig);
            } else {
                logger.info("proxyIp 为null");
                return resultBean;
            }
            Page page = client.getPage(url);
            WebResponse response = page.getWebResponse();
            String requestUrl = response.getWebRequest().getUrl().toString();
            logger.info("url: {}" ,url);
            logger.info("requestUrl: {}",  requestUrl);
            resultBean.setRequestUrl(requestUrl);
            if(response.getStatusCode() == 200){
                pageText = response.getContentAsString(codeType);
            }
            resultBean.setResponseText(pageText);
        } catch (Exception e) {
            if(proxyIP!=null) {
                logger.error("调用WuHttpClient error delete 失败的IP {}", e, proxyIP.toString());
            }
        } finally {
            client.close();
        }
        return resultBean;
    }
}
